﻿package 
{
	import ca.newcommerce.youtube.events.FileUploadEvent;
	import ca.newcommerce.youtube.events.YouTubeEvent;
	import ca.newcommerce.youtube.fileupload.DirectUploader;
	import ca.newcommerce.youtube.webservice.YouTubeDataClient;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	
	/**
	 * ...
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	public class Main extends Sprite 
	{
		protected var _dataWs:YouTubeDataClient;
		protected var _tf:TextField;
		protected var _uploader:DirectUploader;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);		
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			initStage();
			initTf();	
			
			testUpload();
		}
		
		protected function initTf():void
		{
			_tf = new TextField();
			addChild(_tf);
			_tf.name = "results";
			_tf.multiline = true;
			_tf.autoSize = TextFieldAutoSize.LEFT;
			var format:TextFormat = _tf.defaultTextFormat;
			format.size = 26;
			_tf.defaultTextFormat = format;
		}
		
		protected function initStage():void
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			var g:Graphics = graphics;
			g.clear();
			g.lineStyle(0, 0, 0);
			g.beginFill(0xFFFFFF, 1);
			g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			g.endFill();
		}
		
		protected function log(text:String):void
		{
			_tf.text = text;
		}
		
		protected function testUpload():void
		{		
			_dataWs = YouTubeDataClient.getInstance();
			
			_dataWs.login("username", "password");			
			_dataWs.addEventListener(YouTubeEvent.LOGIN_SUCCESSFUL, doLoggedIn);			
		}
		
		protected function doLoggedIn(evt:YouTubeEvent):void
		{
			trace("doLoggedIn");
			_uploader = new DirectUploader();
			_uploader.addEventListener(FileUploadEvent.TOKEN_READY, doTokenReady);
			_uploader.prepareUpload("me video", "video of me", "People", ["arab", "united", "manchester"]);
		}
		
		protected function doTokenReady(evt:FileUploadEvent):void
		{
			
			log("click me");
			addEventListener(MouseEvent.CLICK, doMouseClick);
		}
		
		protected function doMouseClick(evt:MouseEvent):void
		{
			// listen for failed and success
			_uploader.addEventListener(FileUploadEvent.UPLOAD_SUCCEEDED, doFileUploaded);
			_uploader.addEventListener(FileUploadEvent.UPLOAD_FAILED, doFileUploadFailed);
			
			_uploader.browse();
			
			// listen for progress
			_uploader.ref.addEventListener(ProgressEvent.PROGRESS, doUploadProgress);
			
			
			log("i was clicked");
			
			removeEventListener(MouseEvent.CLICK, doMouseClick);
		}		
		
		protected function doFileUploaded(evt:FileUploadEvent):void
		{
			log("id: " + evt.id +"\nmsg: " + evt.msg);
		}
		
		protected function doUploadProgress(evt:ProgressEvent):void
		{
			log("doFileProgress: "+evt.bytesLoaded+" of "+evt.bytesTotal);
		}	
		
		protected function doFileUploadFailed(evt:FileUploadEvent):void
		{
			log("doFileUploadFailed:" + evt.msg);
		}
	}
}